package com.lizy

import org.junit.Test

class ClosureTest {

    @Test
    public void test1() {
        1.upto(10){number -> {
            println(number);
        }}
    }

    /**
     * 习题1
     */
    @Test
    void test2() {
        def list1 = [1,2,3,4,5];
        def list2 = [2,3,4];
        def list = intersect2(list1,list2)
        println(list);
    }
    def intersect(def list1, def list2) {
        def result = list1.intersect(list2){it};
        return result;
    }
    def intersect2(def list1, def list2) {
        def result = list1.findAll(n -> list2.contains(n));
        return result;
    }


    /**
     * 习题2
     */
    @Test
    void test3() {
        def list1 = [1,2,3,4,5];
        def list2 = [2,3,4];
        def list = list1.plus(list2).unique();
        println(list);
    }

    /**
     * 习题3
     */
    @Test
    void test4() {
        def list1 = [1,2,3,4,5];
        def list2 = [2,3,4];
        //def list = list1.findAll {n -> !list2.contains(n)};
        def list = list1 - list2;
        println(list);
    }

    @Test
    void test5(){
        def staff = ['Ken':['John','Peter'],'Jon':['Ken','Jessie'],'Jessie':['Jim','Tom']]

        def name = 'Ken';
        def resultEntry = staff.find { e -> e.getValue().contains(name)}
        println(resultEntry.getKey());

        def leaders = staff.collect {e -> e.getKey()};
        def employee = staff.collect {e -> e.getValue()}.flatten().unique();
        println employee;
        def noSuperiorLeaders = leaders.findAll(n -> !employee.contains(n));
        println(noSuperiorLeaders)
    }

}
